package com.hnust.controller;

import com.hnust.domain.record.CourseChapter;
import com.hnust.domain.record.CourseData;
import com.hnust.domain.record.CourseTargetRecord;
import com.hnust.domain.record.SubjectDataRecord;
import com.hnust.domain.request.SubjectDataListReq;
import com.hnust.service.SubjectService;
import com.hnust.domain.request.SubjectDataReq;
import com.hnust.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 个人信息管理板块接口
 * @author 42
 * @date 2020/10/29
 */

@CrossOrigin("*")
@RestController
@RequestMapping("/api/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    @GetMapping("/getCourseSub")
    public Result< List<SubjectDataRecord>> getCourseSub(@RequestParam(value="course_id",required = false) String course_id,
                                                    @RequestParam(value="token",required = false) String token,
                                                   @RequestParam(value="id",required = false) String id,
                                                     @RequestParam(value="currentPage",required = false) int currentPage,
                                                     @RequestParam(value = "rows",required = false)int rows){
        try{
            List<SubjectDataRecord> subject = subjectService.getQuestionList(course_id,token,id,currentPage,rows);
            if (subject == null){
                return new Result<>(0,"获取题目失败",subject);
            }
            return new Result<>(1,"获取题目成功",subject);
        }catch (Exception e){
            return new Result<>(0,e.getMessage(),null);
        }
    }

    @GetMapping("/getCourseData")
    public Result<List<CourseData>> getCourseData(@RequestParam(value="id",required = false) String id,
                                                  @RequestParam(value="token",required = false) String token){
       List<CourseData> courseData = subjectService.getCourseData(id,token);
        if (courseData == null){
            return new Result<>(0,"获取课程失败",courseData);
        }
        return new Result<>(1,"获取课程成功",courseData);
    }
    @GetMapping("/getCourseSubByCon")
    public Result<List<SubjectDataRecord>> getCourseSubByCon(
                                                    @RequestParam(value = "course_id",required = false)String course_id,
                                                    @RequestParam(value="keyword",required = false) String keyword,
                                                   @RequestParam(value="token",required = false) String token,
                                                   @RequestParam(value="id",required = false) String id,
                                                   @RequestParam(value="currentPage",required = false) int currentPage,
                                                    @RequestParam(value = "rows",required = false)int rows){
            List<SubjectDataRecord> subject = subjectService.getCourseSubByCon(course_id,keyword,token,id,currentPage,rows);
            if (subject == null){
                return new Result<>(0,"获取题目失败",subject);
            }
            return new Result<>(1,"获取题目成功",subject);

    }
    @GetMapping("/delCourseSub")
    public Result<Map<String,Boolean>> delCourseSub(@RequestParam(value = "subject_id",required = false)String subject_id,
                                                    @RequestParam(value = "token",required = false)String token,
                                                    @RequestParam(value = "id",required = false) String id){
        //System.out.println(subject_id);
        Map<String, Boolean> data = new TreeMap<>();
        int code = 0;
        boolean flag = false;
        String msg="删除失败";
        try {
            msg = subjectService.delCourseSub(subject_id,token,id);
        }catch (Exception e){
            data.put("flag",false);
            return new Result<>(code, msg, data);
        }
        if("删除成功".equals(msg)){
            data.put("flag",true);
            code = 1;
        }else {
            data.put("flag",false);
        }
        return new Result<>(code, msg, data);

    }
    @GetMapping("/getCourseTargetList")
    public Result<List<CourseTargetRecord>> getCourseTargetList (@RequestParam(value = "course_id",required = false)String course_id,
                                                                 @RequestParam(value = "token",required = false)String token,
                                                                 @RequestParam(value = "id",required = false) String id){
        List<CourseTargetRecord> courseTargetRecordList = subjectService.getCourseTargetList(course_id,token,id);
        if (courseTargetRecordList == null){
            return new Result<>(0,"获取课程目标失败",courseTargetRecordList);
        }
        return new Result<>(1,"获取课程目标成功",courseTargetRecordList);

    }
    @PostMapping("/updateSubInfo")
    public Result<String> updateSubInfo(@RequestBody SubjectDataReq subjectDataReq){
        System.out.println(subjectDataReq);
        String  msg = subjectService.updateSubInfo(subjectDataReq.getSubjectData(),subjectDataReq.getToken(),subjectDataReq.getId());
        if("修改题目成功".equals(msg)){
            return new Result<>(1,"修改题目成功", null);
        }else {
            return new Result<>(0,"修改题目失败", null);
        }
    }

    @PostMapping("/addSub")
    public Result<String> addSub(@RequestBody SubjectDataListReq subjectDataListReq){
        System.out.println(subjectDataListReq);
        String  msg = subjectService.addSub(subjectDataListReq.getSubjectDataList(),subjectDataListReq.getToken(),subjectDataListReq.getId());
        if("添加题目成功".equals(msg)){
            return new Result<>(1,"添加题目成功", null);
        }else {
            return new Result<>(0,"添加题目失败", null);
        }
    }
    @GetMapping("/getCourseChapterList")
    public Result<List<CourseChapter>> getCourseChapterList(@RequestParam(value = "course_id",required = false)String course_id,
                                                            @RequestParam(value = "token",required = false)String token,
                                                            @RequestParam(value = "id",required = false) String id){
        List<CourseChapter> courseChapterList = subjectService.getCourseChapterList(course_id,token,id);
        if(courseChapterList == null){
            return new Result<>(0,"查询章节失败", courseChapterList);
        }else {
            return new Result<>(1,"查询章节成功", courseChapterList);
        }
    }
}
